package com.he.security.config.annotation.rsocket;

import com.he.security.authentication.ReactiveAuthenticationManager;
import com.he.security.authentication.UserDetailsRepositoryReactiveAuthenticationManager;
import com.he.security.core.userdetails.ReactiveUserDetailsService;
import com.he.security.crypto.password.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class RSocketSecurityConfiguration {
    private static final String BEAN_NAME_PREFIX = "com.he.security.config.annotation.rsocket.RSocketSecurityConfiguration.";

    private static final String RSOCKET_SECURITY_BEAN_NAME = BEAN_NAME_PREFIX + "rsocketSecurity";

    private ReactiveAuthenticationManager authenticationManager;

    private ReactiveUserDetailsService reactiveUserDetailsService;

    private PasswordEncoder passwordEncoder;

    @Autowired(required = false)
    void setAuthenticationManager(ReactiveAuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Autowired(required = false)
    void setUserDetailsService(ReactiveUserDetailsService userDetailsService) {
        this.reactiveUserDetailsService = userDetailsService;
    }

    @Autowired(required = false)
    void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    private ReactiveAuthenticationManager authenticationManager() {
        if (this.authenticationManager != null) {
            return this.authenticationManager;
        }
        if (this.reactiveUserDetailsService != null) {
            UserDetailsRepositoryReactiveAuthenticationManager manager = new UserDetailsRepositoryReactiveAuthenticationManager(this.reactiveUserDetailsService);
            if (this.passwordEncoder != null) {
                manager.setPasswordEncoder(this.passwordEncoder);
            }
            return manager;
        }
        return null;
    }

}
